const httpHelper=require('./httpHelper');
let gps={
    getAddress:async function(latitude,longitude){
        let url = 'http://apis.map.qq.com/ws/geocoder/v1/?location=';
        let key = 'LBMBZ-GBFRX-FC74Y-7HGXA-JUXZF-NVFM3';
        let address='';
        try{
            let resultStr=await httpHelper.getLocal(`${url}${latitude},${longitude}&key=${key}`);
            let result=JSON.parse(resultStr);
            if(result.status==0){
                if(!!result.result.address){
                    address=result.result.address;
                }else if(!!result.result.address_component){
                    address += result.result.address_component.nation;
                    address += result.result.address_component.province;
                    address += result.result.address_component.city;
                    address += result.result.address_component.district;
                    address += result.result.address_component.street;
                    if (result.result.address_component.street != result.result.address_component.street_number) {
                        address += result.result.address_component.street_number;
                    }
                }
            }
        }catch(e){
            console.error(e);
        }
        return address;
    }
};

module.exports=gps;